package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.StudentResultBean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by Administrator on 2016/8/24 0024.
 */
public class StudyResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_TOP = 2;
    private Context context;
    private List<StudentResultBean.DataBean> data;
    private static boolean hasMore = true;
    private int mBottomCount = 1;//底部View个数


    public StudyResultAdapter(Context context, List<StudentResultBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();
        if (mBottomCount != 0 && position >= dataItemCount) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public int getContentItemCount() {
        return data.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {

            View view = LayoutInflater.from(context).inflate(R.layout.item_student_result, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    //判断当前item是否是FooterView
    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= getContentItemCount();
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).studentName.setText(data.get(position).getStudentName());
            ((ItemViewHolder) holder).studentId.setText(data.get(position).getStuId());
            ((ItemViewHolder) holder).studentClassScorese.setText(data.get(position).getClassRoomGrade());
            ((ItemViewHolder) holder).studentInnvoiteScorese.setText(data.get(position).getInnovateGrade());
            Glide.with(context)
                    .load(data.get(position).getStudentUrl())
                    .placeholder(R.drawable.student)
                    .centerCrop()
                    .into(((ItemViewHolder) holder).image);


            if (onItemClickListener != null) {
            }
        } else if (holder instanceof FootViewHolder) {
            if (!hasMore) {
                ((FootViewHolder) holder).loadText.setText("无数据可以加载!");
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).loadText.setText("正在加载...");
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
            }

        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        CircleImageView image;
        TextView studentName;
        TextView studentId;
        TextView studentClassScorese;
        TextView studentInnvoiteScorese;


        public ItemViewHolder(View view) {
            super(view);
            image = (CircleImageView) view.findViewById(R.id.student_image);
            image.bringToFront();
            studentName = (TextView) view.findViewById(R.id.student_name);
            studentId = (TextView) view.findViewById(R.id.student_id);
            studentClassScorese = (TextView) view.findViewById(R.id.student_class_scores);
            studentInnvoiteScorese = (TextView) view.findViewById(R.id.student_innovate_scores);


        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        TextView loadText;
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            loadText = (TextView) view.findViewById(R.id.load_text);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        }
    }


}
